<template>
	<view class="register_device_layout">
		
		<scroll-view scroll-y="true" >
			<view class="list">
				<view class="stepOne" :class=" !stepOne?'active':''">
					<view class="stepNum" :class=" !stepOne?'active':''">
						1
					</view>
					<view class="bgview">
						<view class="top">
							<view class="title" :class=" !stepOne?'active':''">
								{{$t('registerDevice.addImei')}}
							</view>
							<view class="space" style="width:50rpx;">
			
							</view>
							<view class="question-circle">?</view>
							<view class="howfind" @click="findDetailAction">
								How to find device detail?
							</view>
						</view>
			
						<view class="info">
							<!-- Scan the QRCode on your tracker or enter the ID manualy -->
						</view>
			
						<view class="scanBg">
							<text style="color: #333;font-size: 28rpx;margin-top: 10rpx;">扫描二维码</text>
							<text style="color: #666;font-size: 24rpx;margin-top: 10rpx;">可以在包装盒、设备背面或电池下方找到二维码</text>
							<view class="scanImgBg">
								<image style="width: 30%;height: 95rpx;margin-top: 20rpx;" src="/static/device/device_detail.png"
									mode="aspectFill"></image>
							</view>
							<view class="scanButton" @click="toScanCode">
								扫描二维码
							</view>
						</view>
						<view class="Or"
							style="display: flex;justify-content: center;align-items: center; font-size: 36rpx;margin-top: 10rpx;font-weight:700;">
							或者
						</view>
			
						<view class="handInputBg">
							<text style="color: #333;font-size: 28rpx;margin-top: 10rpx;">手动输入</text>
							<text style="color: #666;font-size: 24rpx;margin-top: 10rpx;">设备ICCID可以在包装盒、设备背面或电池下方找到</text>
							<view class="imeiImgBg">
								<image style="width: 55%;height: 90rpx;margin-top: 20rpx;" src="/static/device/device_imei.png"
									mode="aspectFill"></image>
							</view>
							<view class="inputBg">
								<view class="leftText">
									设备ICCID:
								</view>
								<input class="filed" :value="scanImei" placeholder-style="color:#666"
									@input="inputContent" />
							</view>
						</view>
			
					</view>
			
			
				</view>
				<view class="stepTwo otherStep" :class=" !stepTwo?'active':''" @click="stepTwoAction">
					<view class="stepNum" :class=" !stepTwo?'active':''">
						2
					</view>
					<view class="content">
						{{$t('registerDevice.subscribe')}}
					</view>
				</view>
				<!-- <view class="stepThree otherStep" :class=" !stepThree?'active':''" @click="stepThreeAction">
					<view class="stepNum" :class=" !stepThree?'active':''">
						3
					</view>
					<view class="content">
							{{$t('registerDevice.openDevice')}}
						
					</view>
				</view> -->
				<view class="stepFour otherStep" :class=" !stepFour?'active':''" @click="stepFourAction">
					<view class="stepNum" :class=" !stepFour?'active':''">
						3
					</view>
					<view class="content">
						{{$t('registerDevice.setName')}}
					</view>
				</view>
				<view class="stepFive otherStep" :class=" !stepFive?'active':''" @click="stepFiveAction">
					<view class="stepNum" :class=" !stepFive?'active':''">
						4
					</view>
					<view class="content">
						{{$t('registerDevice.setHeadImg')}}
					</view>
				</view>
				<!-- 		<view class="stepSix otherStep" :class=" !stepSix?'active':''" @click="stepSixAction">
					<view class="stepNum" :class=" !stepSix?'active':''">
						6
					</view>
					<view class="content">
						Auto location
					</view>
				</view>
				<view class="stepSeven otherStep" :class=" !stepSeven?'active':''" @click="stepSevenAction">
					<view class="stepNum" :class=" !stepSeven?'active':''">
						7
					</view>
					<view class="content">
						
					</view>
				</view> -->
			</view>
		</scroll-view>
			
		
		<!-- :class=" !stepFive?'active':''" -->

		<view class="bottomBg">
			<view class="nextStep" @click="nextStepAction">
				Next<!-- {{$t('registerDevice.finish')}} -->
			</view>
			<view class="bottom Two">

				<text style="font-size: 28rpx;color: #333;font-weight: 600;">有问题吗？</text>
				<text style="font-size: 28rpx;color: #53786C;font-weight: 700;" @click="connectUs">联系我们</text>
			</view>
		</view>

	</view>

</template>

<script setup>
	import store from '../../store';
	import {
		ref
	} from 'vue';
	import {
		onShow,
		onLoad,
		onUnload,
		onBackPress
	} from "@dcloudio/uni-app";
	import {
		i18nTool
	} from '../../utils/i18en';
	import {
		getUserId
	} from '../../utils/user_tools';
	import {
		apiBindDevice
	} from '../../api/device_api';

	const stepOne = ref(false);
	const stepTwo = ref(false);
	const stepThree = ref(false);
	const stepFour = ref(false);
	const stepFive = ref(false);
	var imei;
	onLoad(() => {
		uni.setNavigationBarTitle({
			title: i18nTool("registerDevice.navTitle")
		})
	})
	onShow(() => {

		// uni.$on("stepOne", function(data) {
		// 	stepOne.value = data.suc;
		// 	imei = data.imei;
		// })
		uni.$on("stepTwo", function(data) {
			stepTwo.value = data;
			let userId = getUserId();
			uni.setStorageSync(userId, imei);
			store.commit('setNewImei', imei);
		})

		// uni.$on("stepThree", function(data) {
		// 	stepThree.value = data;
		// })
		uni.$on("stepFour", function(data) {
			stepFour.value = data;
		})
		uni.$on("stepFive", function(data) {
			stepFive.value = data;
		})
	})
	onUnload(() => {
		// uni.$off("stepOne");
		uni.$off("stepTwo");
		// uni.$off("stepThree");
		uni.$off("stepFour");
		uni.$off("stepFive");
	})
	//添加 Imei
	const stepOneAction = () => {
		uni.navigateTo({
			url: "/pages/add_device/add_device"
		})
	}

	const scanImei = ref(null);
	const inputContent = (v) => {
		scanImei.value = v.detail.value;
		imei = v.detail.value
	}


	function toScanCode() {
		uni.scanCode({
			onlyFromCamera: false,
			scanType: ['barCode', 'qrCode', 'datamatrix', 'pdf417'],
			autoDecodeCharset: true,
			autoZoom: true,
			barCodeInput: true,
			success(res) {
				console.log("扫码结果==", res);
				scanImei.value = res.result;
				imei = res.result;
			},
			fail(err) {

			}

		})
	}

	function connectUs() {
		uni.navigateTo({
			url: "/pages/my/contact_us"
		})
	}

	function findDetailAction() {
		uni.navigateTo({
			url: "/pages/add_device/device-detail-info"
		})
	}
	//订阅
	function stepTwoAction() {
		if (!stepOne.value) {
			uni.showToast({
				title: i18nTool("registerDevice.stepOneErr"),
				icon: "none",
			})
			return;
		}
		uni.navigateTo({
			url: "/pages/subscribe/subscribe?data=" + imei
		})
	}
	//开机
	function stepThreeAction() {
		if (!stepOne.value && !stepTwo.value) {
			uni.showToast({
				title: i18nTool("registerDevice.stepTwoErr"),
				icon: "none",
			})
			return;
		}
		stepThree.value = true;

	}
	//命名
	function stepFourAction() {
		if (!stepTwo.value) {
			uni.showToast({
				title: i18nTool("registerDevice.stepTwoErr"),
				icon: "none",

			})
			return;
		}
		uni.navigateTo({
			url: "/pages/add_device/name-device?data=" + imei
		})
	}
	//设置头像
	function stepFiveAction() {
		if (!stepFour.value) {
			uni.showToast({
				title: i18nTool("registerDevice.stepThreeErr"),
				icon: "none",
			})
			return;
		}
		uni.navigateTo({
			url: "/pages/add_device/set-device-head?data=" + imei
		})
	}



	function nextStepAction() {
		if (!stepOne.value) {
			toRegisterDevice();
		} else if (!stepTwo.value) {
			stepTwoAction()
		} else if (!stepFour.value) {
			stepFourAction()
		} else if (!stepFive) {
			stepFiveAction()
		} else {
			uni.navigateBack();
		}
	}


	const toRegisterDevice = async () => {
		if (imei == null) {
			uni.showToast({
				icon: 'none',
				title: i18nTool('addDeviceImei.title'),
			})
			return;
		}
		const param = {
			imei: imei
		}
		let res = await apiBindDevice(param);
		stepOne.value = true
		if (res.content.subscribed == 1) {
			stepTwo.value = true
			stepFourAction();
		}else{
			stepTwoAction();
		}

	}
</script>

<style lang="scss" scoped>
	.register_device_layout {
		.list {
			margin: 40rpx 40rpx 300rpx 40rpx;
			.stepOne {
				width: 100%;
				position: relative;
				border-left: 4rpx solid #53786C;
				padding-bottom: 50rpx;

				&.active {
					border-left: 4rpx solid #BDBDBD;
				}

				.bgview {
					padding-left: 40rpx;
					margin-top: 5rpx;

					.top {
						position: absolute;
						transform: translate(0, -50%);
						top: 0;
						display: flex;
						justify-content: space-between;
						align-items: center;

						.title {
							font-size: 32rpx;
							font-weight: bold;
							color: #333;
						}

						.question-circle {
							width: 10px;
							height: 10px;
							border-radius: 50%;
							border: 2rpx solid #53786C;
							display: flex;
							align-items: center;
							justify-content: center;
							color: #53786C;
							font-family: Arial, sans-serif;
							font-size: 18rpx;

						}

						.howfind {
							margin-right: -2rpx;
							font-size: 26rpx;
							color: #53786C;
						}
					}

					.info {
						font-size: 24rpx;
					}

					.scanBg {
						margin-top: 30rpx;
						border-radius: 4rpx;
						padding: 10rpx 20rpx;
						background-color: #f1f1f1;
						display: flex;
						flex-direction: column;
						align-items: start;

						.scanImgBg {
							margin-top: 20rpx;
							width: 100%;
							display: flex;
							justify-content: center;
							align-items: center;
							margin-bottom: 20rpx;
						}

						.scanButton {
							width: 100%;
							margin-top: 20rpx;
							margin-bottom: 20rpx;
							position: relative;
							height: 80rpx;
							color: #fff;
							background-color: #53786C;
							border-radius: 40rpx;
							font-weight: 700;
							@include flex-box-set(center);
						}
					}

					.handInputBg {
						margin-top: 10rpx;
						border-radius: 4rpx;
						padding: 10rpx 20rpx;
						background-color: #f1f1f1;
						display: flex;
						flex-direction: column;
						align-items: start;

						.imeiImgBg {
							width: 100%;
							display: flex;
							justify-content: center;
							align-items: center;
							margin-bottom: 20rpx;
							margin-top: 20rpx;
						}

						.inputBg {
							width: 100%;
							margin-top: 20rpx;
							margin-bottom: 20rpx;
							position: relative;
							border: 2rpx solid #BDBDBD;
							background-color: #fff;
							border-radius: 10rpx;
							height: 80rpx;
							align-items: center;
							display: flex;
							justify-content: start;
							.leftText{
								align-items: center;
								margin-left: 4rpx;
								font-size: 28rpx;
							}
							.filed {
								height: 100%;
								padding-left: 30rpx;
								padding-right: 15rpx;
								color: #333;
								font-size: 28rpx;
							}
						}
					}


				}
			}

			.stepNum {
				position: relative;
				left: 0;
				top: 0;
				width: 50rpx;
				height: 50rpx;
				background-color: #53786C;
				border-radius: 50%;
				display: flex;
				text-align: center;
				align-items: center;
				justify-content: center;
				color: #fff;
				transform: translate(-50%, -50%);

				&.active {
					background-color: #BDBDBD;
				}
			}

			.otherStep {
				position: relative;
				height: 70rpx;
				width: 100%;
				border-left: 4rpx solid #53786C;

				&.active {
					border-left: 4rpx solid #BDBDBD;
				}

				.content {
					top: 0;
					position: absolute;
					transform: translate(0, -50%);
					padding: 0 40rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}
			:last-child {
				border-left: 0rpx solid transparent;
				&.active {
					border-left: 0rpx solid transparent;
				}
			}
		}

		.bottomBg {
			position: fixed;
			bottom: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 10rpx;
			background-color: #fff;
			flex-direction: column;
			width: 100%;
			box-shadow : 0 -4px 12px rgba(0,0,0, 0.1 );
			height: 250rpx;
		}

		.nextStep {
			width: 80%;
			height: 80rpx;
			color: #fff;
			background-color: #53786C;
			border-radius: 40rpx;
			margin-top: 20rpx;
			font-size:38rpx;
			font-weight: 700;
			@include flex-box-set(center);

			&.active {
				background-color: #BDBDBD;
			}
		}

		.bottom {
			display: flex;
			align-items: center;
			justify-content: center;
		}
		.One {
			margin-top: 20rpx;
			margin-bottom: 20rpx;
		}
		.Two{
			margin-top: 20rpx;
			margin-bottom: 30rpx;
		}



	}
</style>